package chaw.android.train;

import chaw.android.util.Preferences;
import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class TrainTravelActivity extends TabActivity implements OnTabChangeListener {
    /** Called when the activity is first created. */
	static public TabHost tabHost;
	private LocalBroadcastManager localBroadMgr;
	private BroadcastReceiver newFavoriteReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			checkNewFavorite();
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tabHost = getTabHost();

        Drawable img1 = getResources().getDrawable(R.drawable.nav1_on);
        TabSpec tabSpec1 = tabHost.newTabSpec("Tab1").setIndicator("",img1);
        tabSpec1.setContent(new Intent(this, StationMapActivity.class));
        tabHost.addTab(tabSpec1);
        
        Drawable img2 = getResources().getDrawable(R.drawable.nav2_off);
        TabSpec tabSpec2 = tabHost.newTabSpec("Tab2").setIndicator("",img2);
        tabSpec2.setContent(new Intent(this, StationListActivity.class));
        tabHost.addTab(tabSpec2);
        
        Drawable img3 = getResources().getDrawable(R.drawable.nav3_off);
        TabSpec tabSpec3 = tabHost.newTabSpec("Tab3").setIndicator("",img3);
        tabSpec3.setContent(new Intent(this, TalkActivity.class));
        tabHost.addTab(tabSpec3);
        
        Drawable img4;
        boolean isNewFavorite = Preferences.getBoolean(getBaseContext(), "isNewFavorite");
    	if(isNewFavorite)
    		img4 = getResources().getDrawable(R.drawable.icon_new);
    	else
    		img4 = getResources().getDrawable(R.drawable.nav4_off);
        
        TabSpec tabSpec4 = tabHost.newTabSpec("Tab4").setIndicator("",img4);
        tabSpec4.setContent(new Intent(this, FavoriteActivity.class));
        tabHost.addTab(tabSpec4);
        
        Drawable img5 = getResources().getDrawable(R.drawable.nav5_off);
        TabSpec tabSpec5 = tabHost.newTabSpec("Tab5").setIndicator("", img5);
        tabSpec5.setContent(new Intent(this, MoreActivity.class));
        tabHost.addTab(tabSpec5);
        
        for(int i = 0 ; i < 5; i++)
        	tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_nav1);
        
        tabHost.setCurrentTab(0);
        
        tabHost.setOnTabChangedListener(this);
        
        Preferences.setValue(getBaseContext(), "version", "1.2");
    }
    
    private void checkUser(){
        boolean isRegisted = Preferences.getBoolean(getBaseContext(), "isRegisted");
        if(!isRegisted){
        	startActivity(new Intent(getBaseContext(), RegistNickName.class));
        }
        else{
        	initIntent();
        }
    }
    
    private void initIntent(){
        isChatThenChatRoom();
        isReplyThenBoard();
    }
    
    private void isChatThenChatRoom(){
    	Intent currentIntent = getIntent();
		Bundle bundle = currentIntent.getExtras();
		
		try{
			boolean isChat = bundle.getBoolean("isChat", false);
			if(isChat){
		        tabHost.setCurrentTab(2);
				onTabChanged("Tab3");
				String roomId = bundle.getString("roomId");
				Intent chatIntent = new Intent(getBaseContext(), ChatRoomActivity.class);
				chatIntent.putExtra("roomId", roomId);
				chatIntent.putExtra("isChat", true);
				startActivity(chatIntent);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
    }

    private void isReplyThenBoard(){
    	Intent currentIntent = getIntent();
		Bundle bundle = currentIntent.getExtras();
		
		try{
			boolean isChat = bundle.getBoolean("isReply", false);
			if(isChat){
				String user_id = bundle.getString("writer_id");
				String board_id = bundle.getString("board_id");
				Intent replyIntent = new Intent(getBaseContext(), ProfileBoardViewActivity.class);
				replyIntent.putExtra("user_id", user_id);
				replyIntent.putExtra("board_id", board_id);
				replyIntent.putExtra("isReply", true);
				startActivity(replyIntent);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
    }
    
    private void checkNewFavorite(){
    	boolean isNewFavorite = Preferences.getBoolean(getBaseContext(), "isNewFavorite");
    	ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(3).findViewById(android.R.id.icon);
    	if(isNewFavorite){
    		iv.setImageDrawable(getResources().getDrawable(R.drawable.icon_new));
    	}
    	else{
    		iv.setImageDrawable(getResources().getDrawable(R.drawable.nav4_on));
    	}
    }
    
    private void setLocalCastReceiver(){
		localBroadMgr = LocalBroadcastManager.getInstance(getBaseContext());
        IntentFilter filter = new IntentFilter("newFavorite");
        filter.addCategory("newFavorite");
        localBroadMgr.registerReceiver(newFavoriteReceiver, filter);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		localBroadMgr.unregisterReceiver(newFavoriteReceiver);
	}

	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		int menu_off[] = {R.drawable.nav1_off, R.drawable.nav2_off, R.drawable.nav3_off, R.drawable.nav4_off, R.drawable.nav5_off};
		int menu_on[] = {R.drawable.nav1_on, R.drawable.nav2_on, R.drawable.nav3_on, R.drawable.nav4_on, R.drawable.nav5_on};
		
		for(int i = 0 ; i < tabHost.getTabWidget().getChildCount(); i++){
			ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.icon);
			iv.setImageDrawable(getResources().getDrawable(menu_off[i]));
		}
		ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).findViewById(android.R.id.icon);
		iv.setImageDrawable(getResources().getDrawable(menu_on[tabHost.getCurrentTab()]));
		
		if(tabId.equalsIgnoreCase("Tab4")){
			Preferences.setBoolean(getBaseContext(), "isNewFavorite", false);
//			checkNewFavorite();
		}
		else{
			if (FavoriteActivity.mIdList != null) {
				FavoriteActivity.mIdList.clear();
			}
		}
		
		if(!tabId.equalsIgnoreCase("Tab5"))
			for(int i = 0 ; i < 5; i++)
	        	tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_nav1);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		checkUser();
		
		setLocalCastReceiver();
		isBoardView();
		if(getIntent().getFlags() == FavoriteActivity.PROFILE_VIEW){
			tabHost.setCurrentTab(3);
			onTabChanged("Tab4");
			
			Intent intent = new Intent(getBaseContext(), EachProfileActivity.class);
			intent.putExtra("user_id", getIntent().getExtras().getString("user_id"));
			intent.putExtra("flag", FavoriteActivity.PROFILE_VIEW);
			((FavoriteActivity) getCurrentActivity()).startChildActivity("Profile", intent);
		}
	}
	
	private void isBoardView(){
		Intent currentIntent = getIntent();
		
		try{
			if(currentIntent.getAction().equalsIgnoreCase("BoardViewActivity")){
				tabHost.setCurrentTab(3);
				onTabChanged("Tab4");
				Intent intent = new Intent(getBaseContext(), BoardViewActivity.class);
				intent.putExtras(currentIntent.getExtras());
				startActivity(intent);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}